package top.xcxdl.rural.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import top.xcxdl.rural.entity.RuralExamQuestion;
import top.xcxdl.rural.mapper.RuralExamQuestionMapper;
import top.xcxdl.rural.service.IRuralExamQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ding
 * @since 2022-05-07
 */
@Service
public class RuralExamQuestionServiceImpl extends ServiceImpl<RuralExamQuestionMapper, RuralExamQuestion> implements IRuralExamQuestionService {

    @Override
    public List<Integer> getIdsByExamId(Integer examId) {
        return baseMapper.getIdsByExamId(examId);
    }

    @Override
    public int doEdit(Integer examId, List<Integer> ids) {
        // 先删除
        QueryWrapper<RuralExamQuestion> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id", examId);
        baseMapper.delete(wrapper);
        // 再添加
        int ret = 0;
        if (ids != null && ids.size() > 0) {
            for (Integer id : ids) {
                RuralExamQuestion question = new RuralExamQuestion();
                question.setExamId(examId);
                question.setQuestionId(id);
                ret += baseMapper.insert(question);
            }
        }
        return ret;
    }

    @Override
    public int doDelete(List<Integer> ids) {
        QueryWrapper<RuralExamQuestion> wrapper = new QueryWrapper<>();
        wrapper.in("exam_id", ids);
        return baseMapper.delete(wrapper);
    }
}
